package components
{
	import SEApp.se_eventManager;
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	
	import UIUtils.uif_json;
	
	import classes.clt_events;
	import classes.clt_loader;
	import classes.clt_utils;
	
	import components.dialogs.clt_collages;
	import components.dialogs.clt_headsDialog;
	
	import data.cd_utils;
	import data.cd_workspaces;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.utils.ByteArray;

	public class clt_workspace extends se_eventManager {
		private var _info		: Object;
		private var _maskInfo	: Object;
		private var _app		: clt_clothingBase;
		private var _photoURL	: String;
		private var _collages	: clt_collages;
		
		public function clt_workspace(a_app: clt_clothingBase, target: IEventDispatcher=null) {
			super(a_app, target);
			_app = a_app;
			addEventListener(clt_events.SELECTCUTOUT, doSelectCutOut);
			addEventListener(clt_events.SHOWHEADS, doShowHeads);
			addEventListener(clt_events.GETHEAD, doGetHead);
			addEventListener(clt_events.BANREQUEST, doBanRequest);
			addEventListener(clt_events.USERCOLLAGES, doUserCollages);
			addEventListener(clt_events.SHOWMYCOLLAGES, doMyCollages);
		}
		
		protected function doGetHead(e: ObjectEvent): void {
			e.object.image 	= _photoURL;
			e.object.maskID = maskInfo.id;
		}
		
		protected function doBanRequest(e: ObjectEvent): void {
			
			var contentID	: int;
			var batType		: int = 4;
			
			if (e.object.comment_id) {
				contentID = e.object.comment_id;
				batType = 1;
			} else {
				contentID = e.object.id;
				if (!e.object.hasOwnProperty('noVictory')) {
					_app.alert(_app.classes.locale.WARNING, _app.sitedata.NOBANSET);
					return;
				}
			}
			
			_app.internalServer.banRequest(e.object.uid.toString(), contentID, batType,
				function(a_data: Object): void {
				if (a_data.response[0].result == 1) _app.alert(_app.classes.locale.MESSAGE, _app.sitedata.BANDESCR);
			});
		}
		
		protected function doUserCollages(e: ObjectEvent): void {
			_app.internalServer.getGameList(_app.user.uid + ';time;2000;' + e.object.uid, function(a_data: Object): void {
				_app.externalServer.getProfiles(e.object.uid, function(a_users: Array): void {
					a_data.list = clt_utils.usersUnion(a_users, (a_data.list as Array)?a_data.list:[]);
					showCollagesDialog(se_util.userName(e.object), a_data.list);
				});				
			});			
		}
		
		protected function doMyCollages(e: Event): void {
			_app.internalServer.getGameList(_app.user.uid + ';time;2000;' + _app.user.uid, function(a_data: Object): void {
				a_data.list = clt_utils.usersUnion([_app.user], (a_data.list as Array)?a_data.list:[]);
				showCollagesDialog(se_util.userName(_app.user), a_data.list);
			});			
		}
		
		protected function showCollagesDialog(userName: String, collages: Array): void {
			if (collages.length > 0) {
				if (!_collages) _collages = new clt_collages(_app.dialogLayer);
				_collages.showCollages(collages);
				_collages.title.text = userName;
			} else _app.alert(_app.locale.WARNING, _app.locale.EMPTYCOLLAGELIST);
		}
		
		public function get info(): Object {
			return _info; 
		}
		
		public function get id(): uint {
			return info.id;
		}
		
		public function set id(a_id: uint): void {
			a_id = a_id?a_id:cd_workspaces.defaultItem;
			if (info.id != a_id) {
				info = cd_utils.fromID(cd_workspaces.list, a_id);
				dispatchEvent(new Event(clt_events.DISPLAYREFRESH));
			}
		}
		
		public function get maskInfo(): Object {
			return _maskInfo; 
		}
		
		public function set maskInfo(a_maskInfo: Object): void {
			_maskInfo 	= a_maskInfo;
			waitEvent(new Event(clt_events.UPDATEWORKSPACE));
		}
		
		public function set info(a_info: Object): void {
			_info 		= a_info;
/*			if (!_app.isStartComplete)
				_maskInfo = cd_utils.fromID(cd_masks.list, info.face.mask_id);*/
			waitEvent(new Event(clt_events.UPDATEWORKSPACE));
		}
		
		public function get photoURL(): String {
			return _photoURL;
		}
		
		public function loadPhoto(photo_url: String): void {
			
			var xmlFile: String = photo_url.substr(0, photo_url.indexOf('/', 8)) + '/crossdomain.xml';
			Security.loadPolicyFile(xmlFile);
			
			var ld: clt_loader = new clt_loader(null, photo_url, function(e: Event): void {
				ld.clone(function(e: Event): void {
					_photoURL = photo_url;
					dispatchEvent(new ObjectEvent(clt_events.LOADPHOTOCOMPLETE, e.target.loader));
					e.target.loader.unload();
					ld.dispose();
				});
			}, 0, 0, new LoaderContext(true));
		}
		
		public function loadImageBytes(bytes: ByteArray, event: String): void {
			function doComplete(e: Event): void {
				_photoURL = '';
				dispatchEvent(new ObjectEvent(event, loader));
				loader.unload();
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, doComplete);
			}
			var loader: Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, doComplete);
			loader.loadBytes(bytes);
		}
		
		protected function doSelectCutOut(e: ObjectEvent): void {
			maskInfo = e.object;
		}
		
		protected function doShowHeads(e: Event): void {
			_app.internalServer.getHeads(_app.externalServer.viewer_id, function(a_data: Object): void {
				var list: Array = (a_data.response[0] as Array)?a_data.response[0]:[];
				list.forEach(function(item: Object, index: int, arr: Array): void {
					if (!item.restoreInfo) {
						item.restoreInfo	= uif_json.d((item.mpos as String).replace(new RegExp("'", 'g'), '"'));	
						item.maskID 		= parseInt(item.maskID);
					}
				});
				var dlg: clt_headsDialog = new clt_headsDialog(_app.dialogLayer, list);
				dlg.show();
			});
		}
	}
}